export default {
    namespaced: true,
    state:{
        userAvatar:  uni.getStorageSync('userAvatar') || '',
        nickName: uni.getStorageSync('nickName') || '',
        token: uni.getStorageSync('token')  || ''
    },
    mutations:{
        updateUserAvatar(state,payload){
            state.userAvatar = payload
            uni.setStorageSync('userAvatar', payload)
        },
        updateNickName(state,payload){
            state.nickName = payload
            uni.setStorageSync('nickName', payload)
        },
        updateToken(state,payload){
            state.token = payload
            uni.setStorageSync('token',payload)
        }
    },
    actions:{
        async wxLoginAction(context){
           try{
                // const [error,res] = await uni.getUserInfo()
                // console.log(res);
                const {encryptedData, rawData, iv, signature }  = res
                const [error2,{code}] = await uni.login()
                const data = {
                    encryptedData, rawData, iv, signature, code
                }
                // console.log(data);
                const res2 = await uni.$u.http.post('/users/wxlogin',data)
                // console.log(res2);
                // const token = res2.data.message.token
                const token = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIxMywiaWF0IjoxNjcxOTM5MDE2LCJleHAiOjEwMDE2NzE5MzkwMTV9.08RrN6fmclZLJBVGmOwAulBQQ705x5_xrD_ZYyCjSQ4'
                context.commit('updateToken',token)
                console.log(context);
                uni.showToast({
                    icon: 'success',
                    title: '登录成功~'
                });
           }catch (error) {
                uni.showToast({
                    icon: 'error',
                    title: error
                });
          }
        }
    }
}